package net.server.handler;
import java.io.IOException;
import java.io.OutputStream;


import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;

import core.ChatNode;
import db.Mapper;



public class AskNamesHandler implements HttpHandler
{
	private ChatNode node;
	
	public AskNamesHandler(ChatNode node)
	{
		this.node = node;
	}
	
	@Override
	public void handle(HttpExchange t) throws IOException
	{
		String remoteAddress = t.getRemoteAddress().getAddress().getHostAddress();
		node.getMainWindow().appendText(remoteAddress + " > " + t.getRequestURI());
		
		Mapper userbase = node.getUserBase();
		if(userbase != null)
		{
			String response = userbase.getPeersAsJSON();
			t.sendResponseHeaders(200, response.length());
			OutputStream os = t.getResponseBody();
			os.write(response.getBytes());
			os.close();
		}
		
	}

}
